#include "pageobject.h"
#include "textobject.h"

#include <QtCore/qdebug.h>

int PageObject::m_number = 0;
qreal PageObject::m_offsetY = 0.0;

PageObject::PageObject(QObject *parent) :
    QObject(parent)
{
    setObjectName("_page");
    m_sourceLineNumber = 0;
    m_width = Doker::defaultPageWidth;
    m_height = Doker::defaultPageHeight;
    m_originY = m_offsetY;
    m_offsetY += m_height + Doker::space;
    ++m_number;
}

void PageObject::addText(TextObject *textObject)
{
    textObject->setParent(this);
    m_texts.append(textObject);
}

QRectF PageObject::areaRect(int index) const
{
    QRectF res;
    if (index >= 0 &&  index < m_areas.count())
    {
        res = m_areas.at(index);
        res.translate(0, m_originY);
    }
    return res;
}

QRectF PageObject::frameRect() const
{
    return QRectF(-Doker::space, m_originY - Doker::space,
                  m_width + 2*Doker::space, m_height + 2*Doker::space);
}

QRect PageObject::pixelAreaRect(int index) const
{
    QRectF r = areaRect(index);
    return QRect(toPixels(r.left()), toPixels(r.top()), toPixels(r.width()), toPixels(r.height()));
}

QRect PageObject::pixelFrameRect() const
{
    return QRect(toPixels(-Doker::space), toPixels(m_originY - Doker::space),
                  toPixels(m_width + 2*Doker::space), toPixels(m_height + 2*Doker::space));
}

QRect PageObject::pixelPageRect() const
{
    return QRect(0, toPixels(m_originY), toPixels(m_width), toPixels(m_height));
}

void PageObject::reset()
{
    m_number = 0;
    m_offsetY = 0.0;
}

void PageObject::setHeight(qreal h)
{
    m_offsetY -= m_height;
    m_height = h;
    m_offsetY += m_height;
}

TextObject *PageObject::text(int index) const
{
    TextObject *res = 0;
    if (index >=0 && index < m_texts.count())
    {
        res = m_texts.at(index);
        if (res->y() >= 0.0)
            res->setY(res->y());// + m_originY);
    }
    return res;
}

